package it.heima.xmy;

import java.util.ArrayList;
import java.util.Scanner;

public class MainApp {
    static ArrayList<Goods> goodsArrayList = new ArrayList<>();
    static ArrayList<Order> orderArrayList = new ArrayList<>();
    static ArrayList<User> userArrayList = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);
    static PayInterface pif1 = new Alipay();
    static PayInterface pif2 = new WeChatPay();


    public static void main(String[] args) {

        goodsArrayList.add(0, new Goods("G001", "华为手机", 4000.0, "手机"));
        goodsArrayList.add(1, new Goods("G002", "苹果手机", 8000.0, "手机"));
        goodsArrayList.add(2, new Goods("G003", "HP电脑", 6000.0, "电脑"));
        goodsArrayList.add(3, new Goods("G004", "罗技鼠标", 100.0, "鼠标"));


        User user1 = new Business("张三", "123456");    //商家对象
        User user2 = new Member("李四", "123321");      //会员对象
        userArrayList.add(user1);
        userArrayList.add(user2);

        checkLogin(userArrayList);
    }

    //用户登录
    public static User checkLogin(ArrayList<User> userArrayList) {
        while (true) {
            System.out.println("-------欢迎使用黑马商城-------");
            System.out.println("请输入用户名：");
            Scanner scanner = new Scanner(System.in);
            String inPutName = scanner.next();
            System.out.println("请输入密码：");
            String inPutPassWord = scanner.next();

            for (int i = 0; i < userArrayList.size(); i++) {
                if ((inPutName.equals(userArrayList.get(i).getLoginName())) && (inPutPassWord.equals(userArrayList.get(i).getPassWord()))) {
                    userCheck(userArrayList.get(i));
                    return userArrayList.get(i);
                }
            }
            System.out.println("您输入的用户名或密码有误，请重新输入：");
        }
    }


    //用户判断页面
    public static void userCheck(User user) {

        if (user.getLoginName().equals(userArrayList.get(0).getLoginName())) {
            //商家页面
            showBusinessUI(user, goodsArrayList);
        } else if (user.getLoginName().equals(userArrayList.get(1).getLoginName())) {
            //会员页面
            showMemberUI(user, goodsArrayList, orderArrayList);
        } else {
            System.out.println("您输入的信息有误");
        }
    }


    //商家页面
    public static void showBusinessUI(User user, ArrayList<Goods> goodsArrayList) {
        while (true) {
            System.out.println("-------欢迎" + user.getLoginName() + "进入商户系统-------");
            System.out.println("1.上架商品");
            System.out.println("2.下架商品");
            System.out.println("3.查看商品");
            System.out.println("4.商户退出");
            System.out.println("------------------------");
            System.out.print("请选择功能：");
            Scanner scanner = new Scanner(System.in);

            int inPutChose = scanner.nextInt();
            if (inPutChose == 1) {
                //进入上架商品功能
                goodsUpperShelf(goodsArrayList);
                break;
            } else if (inPutChose == 2) {
                //进入下架商品功能
                goodsDownShelf(goodsArrayList, scanner);
                break;
            } else if (inPutChose == 3) {
                //进入查看商品功能
                findAllGoods(goodsArrayList);
                break;
            } else if (inPutChose == 4) {
                //进入商户退出功能
                System.out.println("继续登录系统请输入Y，否则退出系统");
                String inPutChoice = scanner.next();
                if (inPutChoice.equals("Y")) {
                    checkLogin(userArrayList);
                } else {
                    return;
                }
                break;
            } else {
                System.out.println("您输入的指令有误，请重新输入：");
            }
        }
    }


    //会员页面
    public static void showMemberUI(User user, ArrayList<Goods> goodsArrayList, ArrayList<Order> orderArrayList) {
        System.out.println("------欢迎进入黑马商城-----");
        System.out.println("1.浏览商品");
        System.out.println("2.查看订单");
        System.out.println("3.会员退出");
        while (true) {
            System.out.print("请选择功能：");
            Scanner scanner = new Scanner(System.in);

            int inPutChose = scanner.nextInt();
            if (inPutChose == 1) {
                //商品购买方法

                buyGoods(goodsArrayList, orderArrayList, scanner);
                break;
            } else if (inPutChose == 2) {
                //进入查看订单功能
                findAllOrder(orderArrayList);
                break;
            } else if (inPutChose == 3) {
                //进入会员推出
                System.out.println("继续登录系统请输入Y，否则退出系统");
                String inPutChoice = scanner.next();
                if (inPutChoice.equals("Y")) {
                    checkLogin(userArrayList);
                } else {
                    return;
                }
            } else {
                System.out.println("您输入的指令有误，请重新输入：");
            }
        }
    }


    //商品上架
    public static void goodsUpperShelf(ArrayList<Goods> goodsArrayList) {
        Scanner scanner = new Scanner(System.in);
        Goods goods = new Goods();
        while (true) {
            System.out.println("请输入商品编号：");
            String inPutId = scanner.next();
            if (inPutId.matches("\\w+")) {
                goods.setId(inPutId);
                break;
            } else {
                System.out.println("您输入的商品编号非法,请重新输入：");
            }
        }

        System.out.println("请输入商品名称：");
        String inPutname = scanner.next();
        goods.setName(inPutname);


        System.out.println("请输入商品单价：");

        while (true) {
            try {
                Scanner scanner1 = new Scanner(System.in);
                double inPutPrice = scanner1.nextDouble();
                if (inPutPrice >= 0) {
                    goods.setPrice(inPutPrice);
                }
                break;
            } catch (Exception e) {
                System.out.println("您输入的价格有误，请重新输入：");
            }
        }
        /*   如果存在非法输入的话  应该怎么解决？*/


        System.out.println("请输入商品类别：");
        String inPutCategory = scanner.next();
        goods.setCategory(inPutCategory);
        goodsArrayList.add(goods);
        System.out.println(goods.getName() + "商品上架成功！");
        System.out.println("继续上架请输入Y，否则返回商户操作页面");
        String inPutChoice = scanner.next();
        if (inPutChoice.equals("Y")) {
            goodsUpperShelf(goodsArrayList);
        } else {
            showBusinessUI(userArrayList.get(0), goodsArrayList);
        }
    }


    //商品下架
    public static void goodsDownShelf(ArrayList<Goods> goodsArrayList, Scanner scanner) {
        while (true) {
            System.out.println("请输入您需要下架的商品编号：");
            String inPutId = scanner.next();
            for (int i = 0; i < goodsArrayList.size(); i++) {
                if (inPutId.equals(goodsArrayList.get(i).getId())) {
                    System.out.println("编号为：" + goodsArrayList.get(i).getId() + "的商品已下架！");
                    goodsArrayList.remove(i);
                    System.out.println("是否需要继续下架？输入“Y”键继续，否则任意键返回功能页面。");
                    String inPutChoose = scanner.next();
                    if (inPutChoose.equals("Y")) {
                        goodsDownShelf(goodsArrayList, scanner);
                    } else {
                        showBusinessUI(userArrayList.get(0), goodsArrayList);
                    }
                }
            }
            System.out.println("您输入的编号不存在，请重新输入：");
        }
    }


    //查看商品
    public static void findAllGoods(ArrayList<Goods> goodsArrayList) {
        System.out.println("序号"+"\t\t"+"编号"+"\t\t"+"名称"+"\t\t"+"单价"+"\t\t"+"类别");
        for (int i = 0; i < goodsArrayList.size(); i++) {
            System.out.format("%-8s%-8s%-8s%-8s%s\n",(i+1),goodsArrayList.get(i).getId(),goodsArrayList.get(i).getName(),goodsArrayList.get(i).getPrice(),goodsArrayList.get(i).getCategory());
        }
//        System.out.println("序号      编号      名称      单价      类别      ");
//        for (int i = 0; i < goodsArrayList.size(); i++) {
//            System.out.println((i + 1) + "          " + goodsArrayList.get(i).getId() + "      " + goodsArrayList.get(i).getName() + "      " + goodsArrayList.get(i).getPrice() + " " +
//                    " " + goodsArrayList.get(i).getCategory());
//        }
        System.out.println("--------------");
        showBusinessUI(userArrayList.get(0), goodsArrayList);

    }

    //商品购买
    public static void buyGoods(ArrayList<Goods> goodsArrayList, ArrayList<Order> orderArrayList, Scanner scanner) {
        System.out.println("序号      编号      名称      单价      类别      ");
        for (int i = 0; i < goodsArrayList.size(); i++) {
            System.out.println((i + 1) + "      " + goodsArrayList.get(i).getId() + "      " + goodsArrayList.get(i).getName() + "      " + goodsArrayList.get(i).getPrice() + "      " + goodsArrayList.get(i).getCategory());
        }
        System.out.println("请输入您需要购买的商品序号：");
        int inPutId = scanner.nextInt();
        System.out.println("请输入您需要购买商品的数量：");
        int number = scanner.nextInt();

        boolean flag = false;
        if (inPutId > 0 && inPutId <= (goodsArrayList.size())) {
            System.out.println("请输入您需要支付的方式：");
            System.out.println("1.支付宝：");
            System.out.println("2.微信：");
            System.out.println("3.暂不支付");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    while (true) {
                        System.out.println("总金额为："+number * goodsArrayList.get(inPutId - 1).getPrice());
                        System.out.println("请输入您需要支付的金额：");
                        double inPutMoney = scanner.nextDouble();
                        if (inPutMoney == (number * goodsArrayList.get(inPutId - 1).getPrice())) {
                            flag = pif1.pay(number * goodsArrayList.get(inPutId - 1).getPrice());
                            System.out.println("支付成功");
                            break;
                        } else {
                            System.out.println("支付失败，请重新输入：");
                        }
                    }
                    break;
                case 2:
                    while (true) {
                        System.out.println("总金额为："+number * goodsArrayList.get(inPutId - 1).getPrice());
                        System.out.println("请输入您需要支付的金额：");
                        double inPutMoney1 = scanner.nextDouble();
                        if (inPutMoney1 == (number * goodsArrayList.get(inPutId - 1).getPrice())) {
                            flag = pif2.pay(number * goodsArrayList.get(inPutId - 1).getPrice());
                            System.out.println("支付成功");
                            break;
                        } else {
                            System.out.println("支付失败，请重新输入：");
                        }
                    }
                    System.out.println("支付成功");
                    break;
                default:
                    System.out.println("暂不支付");
                    break;
            }

            //订单集合添加元素
            Order order = new Order(goodsArrayList.get(inPutId - 1),
                    number, number * goodsArrayList.get(inPutId - 1).getPrice(), flag);
            orderArrayList.add(order);
        }
        System.out.println("是否继续选择购买？输入“Y”继续购买，否则退出");
        String inPutChoice = scanner.next();
        if (inPutChoice.equals("Y")) {
            buyGoods(goodsArrayList, orderArrayList, scanner);
        } else {
            showMemberUI(userArrayList.get(1), goodsArrayList, orderArrayList);
        }
    }


    //查看订单
    public static void findAllOrder(ArrayList<Order> orderArrayList) {
        if (orderArrayList.size() == 0) {
            System.out.println("你还没有购买任何东西");
        } else {
            System.out.println("编号      名称        单价        类别         数量       总金额       支付状态");
            for (int i = 0; i < orderArrayList.size(); i++) {
                System.out.println(orderArrayList.get(i).getGoods().getId() + "     " +
                        orderArrayList.get(i).getGoods().getName() + "     " +
                        orderArrayList.get(i).getGoods().getPrice() + "     " +
                        orderArrayList.get(i).getGoods().getCategory() + "     " +
                        orderArrayList.get(i).getAmount() + "     " +
                        ((orderArrayList.get(i).getGoods().getPrice()) * (orderArrayList.get(i).getAmount())) + "     " +
                        orderArrayList.get(i).isIsPay());
            }
        }
        System.out.println("是否继续选择购买？输入“Y”继续购买，否则退出");
        String inPutChoice = scanner.next();
        if (inPutChoice.equals("Y")) {
            buyGoods(goodsArrayList, orderArrayList, scanner);
        } else {
            showMemberUI(userArrayList.get(1), goodsArrayList, orderArrayList);
        }
    }

}
